﻿@{
    Layout = null;
}
@using CNKI.TPI.Web.Search.Model;
@using CNKI.TPI.Web.Base
<script>
    //组全选
    function groupcheckall(e) {
        if ($(e).attr("checked") == "checked") {
            $(".checkboxgrooup input[type='checkbox']").each(function () {
                $(this).attr("checked", "checked");
            })
        }
        else {
            $(".checkboxgrooup input[type='checkbox']").each(function () {
                $(this).removeAttr("checked", "checked");
            })
        }
    }
    $(".checkboxgrooup input[type='checkbox']").click(function () {
        //循环判断列表中的checkbox，如果全部选中则全选框呈勾选状态
        var m = 0;
        if ($(this).attr("checked") != "checked") {
            $("input.groupall").removeAttr("checked", "checked");
        }
        else {
            $(".checkboxgrooup input[type='checkbox']").each(function () {
                if ($(this).attr("checked") != "checked") {
                    m = 1;
                }
            })
            if (m == 1) {
                $("input.groupall").removeAttr("checked", "checked");
            }
            else {
                $("input.groupall").attr("checked", "checked");
            }
        }

    })
    //删除租 多选
    function delgrouplist() {
        var ckbs = $(".checkboxgrooup input[type='checkbox']:checked");
        if (ckbs.size() == 0) {
            messageAlert("请选择要删除的记录！");
            return;
        }
        confirmDialog('确定要删除吗?', function () {
            var checkedstr = [];
            ckbs.each(function () {
                checkedstr.push($(this).val());
            })
            var delurl = "@Html.Raw(Url.Action("delUserGroup", "Admin_User"))";
            var data = { "idlist": checkedstr.toString() };
            dealdata(delurl, data);
        })

    }
    //添加组
    function addgroup() {
        $("#FlageID").val(0);
        $(".talkDiv-title span").html("新建组");
        $("#groupname").val("");
        $("#groupdescript").val("");
        $("#iptable tbody").html("");
        var defaultstr=" <tr><td class=\"checkIp\"> <input type=\"checkbox\" class=\"f-checkbox\" onclick=\"iPclick(this)\"></td>"+
                        "<td><input value=\"0.0.0.0\" /></td>"+
                        "<td><input value=\"255.255.255.255\" /></td></tr>";
        $("#iptable tbody").append(defaultstr);//默认增加一个IP
        showgroup();
    }
    //编辑
    function modifygroup(sysid, name, descript) {
        $("#FlageID").val(sysid);
        $(".talkDiv-title span").html("编辑组");
        $("#groupname").val(name);
        $("#groupdescript").val(descript);

        //获取IP
        $("#iptable tbody").html("");
        $.ajax({
            type: "GET",
            url: "@Html.Raw(Url.Action("GetGroupIpList", "Admin_User"))?groupid=" + sysid,
            success: function (m) {
                if (m != null && m != "undefined") {
                    //;
                    for (var i = 0; i < m.length; i++) {
                        checkcolumn= "<tr><td class=\"checkIp\"> <input type=\"checkbox\" class=\"f-checkbox\" onclick=\"iPclick(this)\"></td>"
        + "<td><input type=\"text\" value="+m[i].SIP+" class=\"f-text span2\" maxlength=\"20\"> </td>"
        + " <td><input type=\"text\" value=" + m[i].EIP + " class=\"f-text span2\" maxlength=\"20\"></td></tr>"
                        $("#iptable tbody").append(checkcolumn);
                    }
                }
            },
            error: function (e)
            { }
        })
        showgroup();
    }
    //删除组
    function delgroup(sysid) {
        var delurl = "@Html.Raw(Url.Action("DelUserGroup", "Admin_User"))";
        var data = { "idlist": sysid };
        confirmDialog('确定要删除吗?', function () {
            dealdata(delurl, data);
        })
    }
    function closegroup() {
        $(".talkDivBg").hide();
        $(".groupform").hide();
    }
    function showgroup() {
        $("input.ipall").removeAttr("checked", "checked");
        $(".talkDivBg").show();
        $(".groupform").show();
    }
    //调用controll
    function dealdata(urlstr, datastr) {
        $.ajax({
            type: "POST",
            url: urlstr,
            data: datastr,
            success: function (m) {
                if (m == "True") {
                    messageAlert("操作成功！");
                    ajaxLoad("initload", '@Html.Raw(Url.Action("GroupIndex", "Admin_User"))');
                }
                else {
                    messageAlert("操作失败！");
                }
            },
            error: function (e)
            { }
        })
    }
    function textIP() {
        var m = 0;
        $('#iptable tbody tr').each(function () {
            //;
            var startip = $(this).find("td").eq(1).find("input").val();
            var endip = $(this).find("td").eq(2).find("input").val();
            if (startip == "" || endip == "") {
                messageAlert("IP为空值，请录入或者删除！");
                m = 1;
                return false; //跳出循环
            }
            else {
                if (!validateIP(startip)) {
                    messageAlert('您输入起始IP格式不正确!');
                    m = 1;
                    return false;
                }
                if (!validateIP(endip)) {
                    messageAlert('您输入终止IP格式不正确!');
                    m = 1;
                    return false;
                }

            }
        })
        if (m == 1) {
            return false;
        }
        return true;

    }
    function savegroup() {
        var groupname = $("#groupname").val();
        var groupdesp = $("#groupdescript").val();
        if ($.trim(groupname) == "") {
            messageAlert("请输入组名称！");
            return;
        }
        else {
            if (!regtext(groupname)) {
                messageAlert("请输入组名称(不允许输入特殊字符)！");
                return;
            }
        }
        if (groupdesp != "") {
            if (!regtext(groupdesp)) {
                messageAlert("请输入组描述(不允许输入特殊字符)！");
                return;
            }
        }
        //用户IP判断
        if (textIP()) {
            var sysid = $("#FlageID").val();
            var m = 0; //判断组名是否重复
            $.ajax({
                type: "GET",
                async: false,
                url: "@Html.Raw(Url.Action("IsExitUserGroup", "Admin_User"))?name=" + groupname + "&sysid=" + sysid,
                success: function (obj) {
                    if (obj == 1) {
                        messageAlert("该组名已经存在！");
                        m = 1;
                    }
                },
                error: function (e)
                { }
            })
            if (m == 0) {
                //用户IP
                var jsonip = [];//用户IP
                $('#iptable tbody tr').each(function () {
                    var iplist = {
                        "SIP": $(this).find("td").eq(1).find("input").val(),
                        "EIP": $(this).find("td").eq(2).find("input").val(),
                        "Flag": "1"
                    }
                    jsonip.push(iplist);
                })
                var insertdata = { "Name": groupname, "Descript": groupdesp, "Type": "4", "SysID": sysid };
                var saveurl = "@Html.Raw(Url.Action("SaveUserGroup", "Admin_User"))";
                var savedata = { "obj": insertdata,"iplist":jsonip };
                $.ajax({
                    type: "POST",
                    url: saveurl,
                    data: JSON.stringify(savedata),
                    contentType: "application/json",
                    success: function (m) {
                        if (m == "True") {
                            messageAlert("操作成功！");
                            closegroup();
                            ajaxLoad("initload", '@Html.Raw(Url.Action("GroupIndex", "Admin_User"))');
                        }
                        else {
                            messageAlert("操作失败！");
                        }
                    },
                    error: function (e)
                    { }
                })
            }
        }
    }
    //数据库权限设置
    function dbAuth(dbid) {
        ajaxLoad("initload", '@Html.Raw(Url.Action("DBAuthList", "Admin_User"))?id=' + dbid + '&flage=1');  //flage  1代表用户组数据库权限，0代表用户数据库权限设置
    }

    //用户组IP操作
    //添加字段
    function addIP() {
        var checkcolumn = "<tr><td class=\"checkIp\"> <input type=\"checkbox\" class=\"f-checkbox\" onclick=\"iPclick(this)\"></td>"
       + "<td><input type=\"text\" class=\"f-text span2\" maxlength=\"20\"> </td>"
       + " <td><input type=\"text\" class=\"f-text span2\" maxlength=\"20\"></td></tr>"
        $("#iptable tbody").append(checkcolumn);

    }
    //删除字段
    function delIP() {
        var ckbs = $(".checkIp input[type='checkbox']:checked");
        if (ckbs.size() == 0) {
            messageAlert("请选择要删除的记录！");
            return;
        }
        confirmDialog('确定要删除吗?', function () {
            ckbs.each(function () {
                //遍历删除
                $(this).parent().parent().remove();
            })
            $(".ipall").removeAttr("checked", "checked");
        })


    }
    ///全选
    function ipcheckall(e) {
        if ($(e).attr("checked") == "checked") {
            $(".checkIp input[type='checkbox']").each(function () {
                $(this).attr("checked", "checked");
            })
        }
        else {
            $(".checkIp input[type='checkbox']").each(function () {
                $(this).removeAttr("checked", "checked");
            })
        }
    }

    function iPclick(e) {
        //循环判断列表中的checkbox，如果全部选中则全选框呈勾选状态
        if ($(e).attr("checked") != "checked") {
            $(".ipall").removeAttr("checked", "checked");
        }
        else {
            dealchecked(".checkIp input[type='checkbox']", ".ipall");
        }
    }
    function dealchecked(checkedsub, checkedparent) {
        var m = 0;
        $(checkedsub).each(function () {
            if ($(this).attr("checked") != "checked") {  //遍历判断是否存在没有选中的记录
                m = 1;
            }
        })
        if (m == 1) {
            $(checkedparent).removeAttr("checked", "checked");
        }
        else {
            $(checkedparent).attr("checked", "checked");
        }
    }
</script>
<table width="100%" cellspacing="0" cellpadding="0" border="0" class="table table-grey">
    <thead>
        <tr class="thead">
            <th class="tdnum">
                <input type="checkbox" class="groupall" value="" name="" onclick="groupcheckall(this)">序号</th>
            <th class=''>组名</th>
            <th class='tl'>描述</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
        @{if (ViewBag.GroupList != null)
          {
              int sortno = ViewBag.PageSize * (ViewBag.PageNow - 1) + 1;
              foreach (SYS_CMS_GROUP item in ViewBag.GroupList)
              {
                  string str = "even";
                  if (sortno % 2 == 0)
                  {
                      str = "odd";
                  }
            <tr class='@str'>
                <td class="checkboxgrooup">
                    @{   string name = ColumnAliasHelper.GetContentNoRed(item.Name).ToUpper();
                         if (name != "ADMINISTRATORS" && name != "ANONYMOUS"
                      && name != "USERS" && name != "OPERATORS" && name != "DBCREATORS")
                         {
                        <input type="checkbox" value="@item.SysID"  >@sortno
                         }
                         else
                         {
                           @sortno
                         }
                    }
                <td>@Html.Raw(item.Name)</td>
                <td>
                    <a class="atitle" title="@item.Descript">@Html.Raw(ColumnAliasHelper.SubContent(item.Descript, 15))</a>

                </td>
                <td class="tc">
                    @if (name == "ADMINISTRATORS")
                    {
                        <a style="visibility: hidden" href="javascript:void(0)">数据库权限设置</a>
                    }
                    else
                    {
                         <a  href="javascript:void(0);" onclick="dbAuth('@item.SysID')">数据库权限设置</a>
                    }
                    <a onclick="modifygroup('@item.SysID','@item.Name','@item.Descript')" href="javascript:void(0)">编辑</a>
                    @if (name == "ADMINISTRATORS" || name == "ANONYMOUS"
                     || name == "USERS" || name == "OPERATORS" || name == "DBCREATORS")
                    {
                        <a style="visibility: hidden" href="javascript:void(0)">删除</a>
                    }
                    else
                    {
                        <a onclick="delgroup('@item.SysID')" href="javascript:void(0)">删除</a>
                    }

                </td>
            </tr>
                    sortno = sortno + 1;
              }
          }

        }
    </tbody>
</table>

@*//分页*@
<div class="pagenav pagenav-02">
    @*上一页*@
    @if (ViewBag.TotalPage > 0)
    {
        if (ViewBag.PageNow != 1)
        {
            int thispage = ViewBag.PageNow - 1;
        <a href="javascript:void(0);" class="page-prev" onclick="searchPageNew('@thispage')" >上一页</a>
        }
        else
        {
        <a href="javascript:return false;"  onclick="return false" class="page-prev disable">上一页</a>
        }
@*循环初始化页码*@
        foreach (string numpage in ViewBag.PageList)
        {
            if (numpage != "0")
            {
                int m = int.Parse(numpage);
                if (m == ViewBag.PageNow)
                {
        <a href="javascript:void(0);" class="active" onclick="searchPageNew('@numpage')">@numpage</a>
                    
                }
                else
                {
        <a href="javascript:void(0);" onclick="searchPageNew('@numpage')">@numpage</a>
                }

            }
            else
            {
        <i class="iconfont icon-dian2"></i>
            }

        }
@*下一页*@
        if (ViewBag.TotalPage > 0 && ViewBag.PageNow != ViewBag.TotalPage)
        {
            int nextpage = ViewBag.PageNow + 1;
        <a href="javascript:void(0);" class="page-next" onclick="searchPageNew('@nextpage')" >下一页</a>
        }
        else
        {
        <a href="javascript:return false;"  onclick="return false" class="page-prev disable">下一页</a>
        }
    }
    @if (@ViewBag.Count == 0)
    {
        <label>暂无数据！</label>
        
    }
    else
    {
          
        <label>共 </label>
        <label>@ViewBag.Count </label>
        <label>条数据 </label>
    }
</div>
<div class='posr' style="padding-bottom: 20px">

    <a class="btn btn-m btn-submit btn-defa-f" href="javascript:void(0);" onclick="addgroup()">添加</a>
    <a class="btn btn-m btn-submit btn-defa-f" href="javascript:void(0);" onclick="delgrouplist()">删除</a>
</div>


<!--talkDiv弹出层(不带图片，宽550)-->
<!--透明背景-->
    <div class="talkDivBg" style="display: none;">
        <div class="talkDiv talkDivTr talkW-550 form-h groupform" style='margin-top: -260px; display: none'>
            <div class="talkDiv-title">
                <span>新建组</span>
                <a class="icon icon-close" title="关闭" onclick="closegroup()" href="javascript:void(0);"><i class="iconfont icon-error3"></i></a>
            </div>
            <div class="talkDiv-body talkDiv-body1">
                 <h4 class='box-h box-h-noborder'>用户组基本信息
        </h4>
                <div class="f-gp">
                    <div class="f-lb">
                        <label class="f-label">组名称：</label>
                    </div>
                    <div class="f-ct">
                        <input type="text" value="" class="f-text span5" id="groupname" maxlength="64">
                        <span class="f-label-re">*</span>
                    </div>
                </div>

                <div class="f-gp">
                    <div class="f-lb">
                        <label class="f-label">组描述：</label>
                    </div>
                    <div class="f-ct">
                        <textarea rows="4" class="f-textarea span5" id="groupdescript" maxlength="254"></textarea>
                    </div>
                </div>
                  <div class="box">
        <h4 class='box-h box-h-noborder'>用户组IP设置 
        </h4>
        <div class="btn-group clearfix inline-block ml10" style="padding-top: 10px; padding-bottom: 10px">
            <a class="btn btn-white fl" href="javascript:void(0);" onclick="addIP()">添加</a>
            <a class="btn btn-white" style="margin-left: 10px" href="javascript:void(0);" onclick="delIP()">删除</a>
        </div>
          <div style="height:230px;overflow:auto;">
        <table width="100%" cellspacing="0" cellpadding="0" border="0" class="table table-grey" id="iptable" style="margin-bottom:20px;">
            <thead class='th-wr'>
                <tr class="thead">
                    <th class="tdnum tdnum1">
                        <input class="ipall" type="checkbox" onclick="ipcheckall(this)">序号</th>
                    <th class='tc'>起始IP</th>
                    <th>终止IP</th>
                </tr>
            </thead>
            <tbody>
              
            </tbody>
        </table>
</div>
    </div>
                <div class="f-gp">
                    <div class="f-lb">
                        <label class="f-label"></label>
                    </div>
                    <div class="f-ct" style="margin-left: 150px">
                        <a href="javascript:void(0);" class="btn btn-m btn-prim-f btn-submit" onclick="savegroup()">保存</a>
                        &#12288;<a href="javascript:void(0);" class="btn btn-m btn-submit btn-defa-f" onclick="closegroup()">取消</a>
                    </div>
                </div>

            </div>
        </div>
</div>